/*
 * @Description: 社群页面
 * @Author: iamsmiling
 * @Date: 2021-05-26 08:20:20
 * @LastEditTime: 2021-06-08 21:34:20
 */
import 'package:credit_business_circle/R/R.dart';
import 'package:credit_business_circle/components/image/chimera_image.dart';
import 'package:credit_business_circle/domain/remote/community/community_entity.dart';
import 'package:credit_business_circle/pages/community/community_controller.dart';
import 'package:credit_business_circle/routes/app_routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CommunityPage extends StatelessWidget {
  const CommunityPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: R.color.ffffffff,
        title: Text(
          "社群",
          style: TextStyle(color: R.color.ff111111, fontSize: R.dimen.sp15),
        ),
      ),
      body: GetBuilder<CommunityController>(
          init: CommunityController(),
          builder: (_) {
            return CustomScrollView(
              slivers: [
                SliverToBoxAdapter(
                  child: Image.asset(
                    R.image.communityBanner,
                    width: R.dimen.width,
                    fit: BoxFit.fill,
                  ),
                ),
                SliverToBoxAdapter(
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: R.dimen.dp10),
                    child: Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(
                              top: R.dimen.dp26, bottom: R.dimen.dp16),
                          child: Row(
                            children: [
                              Text(
                                "可用入群卡：",
                                style: TextStyle(
                                    color: R.color.ff111111,
                                    fontSize: R.dimen.sp16,
                                    fontWeight: FontWeight.w600),
                              ),
                              Text(
                                "0张",
                                style: TextStyle(
                                    color: R.color.fff97703,
                                    fontSize: R.dimen.sp16,
                                    fontWeight: FontWeight.w600),
                              ),
                              Spacer(),
                              Container(
                                padding: EdgeInsets.symmetric(
                                    horizontal: R.dimen.dp16,
                                    vertical: R.dimen.dp4),
                                decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.circular(R.dimen.sp20),
                                    gradient: LinearGradient(colors: [
                                      R.color.ff33cda0,
                                      R.color.ff25b48a
                                    ])),
                                child: Text(
                                  "购买",
                                  style: TextStyle(
                                      color: R.color.ffffffff,
                                      fontSize: R.dimen.sp13),
                                ),
                              )
                            ],
                          ),
                        ),
                        Divider()
                      ],
                    ),
                  ),
                ),
                SliverPadding(
                  padding: EdgeInsets.symmetric(horizontal: R.dimen.dp10),
                  sliver: SliverList(
                      delegate: SliverChildBuilderDelegate(
                          (BuildContext context, int i) {
                    return CommunityGroupCard(group: _.communiies[i]);
                  }, childCount: _.communiies.length)),
                )
              ],
            );
          }),
    );
  }
}

class CommunityGroupCard extends StatelessWidget {
  final CommunityEntity group;
  const CommunityGroupCard({Key? key, required this.group}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => Get.toNamed(AppRoutes.communityGroup),
      child: Container(
        height: R.dimen.dp100,
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: R.dimen.dp22),
              child: Row(
                children: [
                  Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(right: R.dimen.dp20),
                    child: ChimeraImage(
                      src: group.avatar,
                      width: R.dimen.dp55,
                      height: R.dimen.dp55,
                      borderRadius: BorderRadius.zero,
                    ),
                  ),
                  Expanded(
                      child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            group.title,
                            style: TextStyle(
                                fontSize: R.dimen.sp15,
                                color: R.color.ff333333,
                                fontWeight: FontWeight.w600),
                          ),
                          CommunityGroupActionChip(group: group)
                        ],
                      ),
                      Container(
                        margin: EdgeInsets.only(top: R.dimen.dp18),
                        child: Text(
                          group.description,
                          style: TextStyle(
                              fontSize: R.dimen.sp11, color: R.color.ff888888),
                        ),
                      ),
                    ],
                  ))
                ],
              ),
            ),
            Spacer(),
            Divider(
              indent: R.dimen.dp20 + R.dimen.dp55,
            )
          ],
        ),
      ),
    );
  }
}

class CommunityGroupActionChip extends StatelessWidget {
  final CommunityEntity group;

  const CommunityGroupActionChip({Key? key, required this.group})
      : super(key: key);

  String get label {
    if (group.joined) {
      return "已加入";
    }
    if (group.full) {
      return "已满";
    }
    if (group.free) {
      return "入群";
    }
    return "付费加入";
  }

  List<Color> get colors {
    if (group.joined || group.full) {
      return [R.color.ffb7b7b6, R.color.ffcdcdcb];
    }
    return [R.color.ff07aaee, R.color.ff3bc5f8];
  }

  double get fontSize => label.length > 3 ? 10 : 12;

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      width: R.dimen.dp55,
      height: R.dimen.dp21,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(R.dimen.sp10),
          gradient: LinearGradient(colors: colors)),
      child: Text(
        label,
        style: TextStyle(fontSize: fontSize, color: R.color.ffffffff),
      ),
    );
  }
}
